package com.hxs.learn.service.core;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hxs.learn.common.entity.TreeJson;
import com.hxs.learn.common.page.Page;
import com.hxs.learn.common.page.PageHelper;
import com.hxs.learn.common.page.PageWrapper;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.constants.core.sql.SqlColConts;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hxs.learn.entity.core.SysMenuPermission;
import com.hxs.learn.mapper.core.SysMenuPermissionMapper;

/**
 * @author huxinsheng
 * @data 2018-04-24
 */
@Service
public class SysMenuPermissionService {

    private Logger logger = LoggerFactory.getLogger(SysMenuPermissionService.class);

    private final SysMenuPermissionMapper sysMenuPermissionMapper;

    @Autowired
    public SysMenuPermissionService(SysMenuPermissionMapper sysMenuPermissionMapper) {
        this.sysMenuPermissionMapper = sysMenuPermissionMapper;
    }

    public List<TreeJson<Object>> getMenuPermissionTreeJson(String menuId) {
        return sysMenuPermissionMapper.selectByMenuId(menuId);
    }

    /**
     * 分页查询
     *
     * @param page              分页对象
     * @param sysMenuPermission 分页条件对象
     * @return PageWrapper<SysMenuPermission>
     */
    public PageWrapper<SysMenuPermission> findSysMenuPermissionPageData(Page page, SysMenuPermission sysMenuPermission) {
        return PageHelper.getPageData(sysMenuPermissionMapper, page, sysMenuPermission);
    }

    /**
     * 查询所有
     *
     * @return List<SysMenuPermission>
     */
    public List<SysMenuPermission> findAllSysMenuPermission() {
        return sysMenuPermissionMapper.selectList(new EntityWrapper<>(new SysMenuPermission()));
    }

    /**
     * 按主键查询
     *
     * @param id 主键id
     * @return SysMenuPermission
     */
    public SysMenuPermission findById(Integer id) {
        return sysMenuPermissionMapper.selectById(id);
    }

    /**
     * 保存
     *
     * @param sysMenuPermission
     */
    @Transactional(rollbackFor = Exception.class)
    public Result save(LoginInfo loginUser, SysMenuPermission sysMenuPermission) {
        //取出请求数据做后续处理
        String[] permissionIds = StringUtils.split(sysMenuPermission.getPermissionId(), ",");
        String menuId = sysMenuPermission.getMenuId();

        if (ArrayUtils.isEmpty(permissionIds)) {
            return Result.error("该菜单未添加任何权限");
        }
        EntityWrapper<SysMenuPermission> wrapper = new EntityWrapper<>();
        wrapper.eq(SqlColConts.MENU_ID, menuId);
        //先删除之前关联的菜单数据
        sysMenuPermissionMapper.delete(wrapper);
        List<SysMenuPermission> pojos = new ArrayList<>(permissionIds.length);
        for (String id : permissionIds) {
            SysMenuPermission pojo = new SysMenuPermission();
            pojo.setPermissionId(id);
            pojo.setMenuId(menuId);
            pojo.setCreateTime(new Date());
            pojo.setCreator(loginUser.getLoginName());
            pojos.add(pojo);
        }
        sysMenuPermissionMapper.insertAll(pojos);
        return Result.ok();
    }

    /**
     * 按主键删除
     *
     * @param id 主键id
     */
    @Transactional(rollbackFor = Exception.class)
    public Result removeById(Integer id) {
        sysMenuPermissionMapper.deleteById(id);
        return Result.ok();
    }
}
